package com.tuanzi.loan.business.service.family.impl;

import com.base.common.log.LogService;
import com.tuanzi.loan.business.constants.DictionaryCode;
import com.tuanzi.loan.business.entity.CurrentUser;
import com.tuanzi.loan.business.entity.family.LoanLargeDeliveryManagement;
import com.tuanzi.loan.business.repository.family.LoanLargeDeliveryManagementRepository;
import com.tuanzi.loan.business.service.family.LoanLargeDeliveryManagementService;
import com.tuanzi.loan.business.service.system.DictionaryService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.repository.JPAAuditRepository;
import com.tuanzi.loan.core.service.BaseAuditServiceImpl;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.family.LoanLargeDeliveryManagementSearchRequest;
import com.tuanzi.loan.vo.response.family.LoanLargeDeliveryManagementSearchResponse;
import com.tuanzi.server.loan_base.constant.LoanRedisKey;
import com.tuanzi.server.loan_base.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;

@Debug
@Service
@Transactional
@Retryable(value = {RetryException.class})
public class LoanLargeDeliveryManagementServiceImpl extends BaseAuditServiceImpl<LoanLargeDeliveryManagement, Long> implements LoanLargeDeliveryManagementService {
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    @Autowired
    private DictionaryService service;

    @Autowired
    LoanLargeDeliveryManagementRepository repository;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public Page<LoanLargeDeliveryManagementSearchResponse> list(PageSearcher<LoanLargeDeliveryManagementSearchRequest> pageSearcher) {
        return repository.list(pageSearcher.getSearchCondition(),pageSearcher.getPageable());
    }

    @Override
    public LoanLargeDeliveryManagement saveLoanLargeDeliveryManagement(LoanLargeDeliveryManagementSearchRequest request) {
        deleteRedis(request.getType());
        LoanLargeDeliveryManagement loanLargeDeliveryManagement = new LoanLargeDeliveryManagement();
        loanLargeDeliveryManagement.setAmountinterval(request.getAmountinterval());
        loanLargeDeliveryManagement.setCreator(CurrentUser.get().getName());
        try {
            loanLargeDeliveryManagement.setCreatedate(sdf.parse(DateUtils.getCurrentDate()));
        } catch (ParseException e) {
           LogService.error("日期转换异常",e);
        }
        loanLargeDeliveryManagement.setCreateTime(DateUtils.formatYyyyMMddHHmmssToDate(DateUtils.getCurrentTime()));
        loanLargeDeliveryManagement.setDelivermethod(request.getDelivermethod());
        loanLargeDeliveryManagement.setOwnner(request.getOwnner());
        loanLargeDeliveryManagement.setSort(request.getSort());
        loanLargeDeliveryManagement.setType(request.getType());
        loanLargeDeliveryManagement.setMaxamount(request.getMaxamount());
        loanLargeDeliveryManagement.setMinamount(request.getMinamount());
        return repository.saveAndFlush(loanLargeDeliveryManagement);
    }

    @Override
    public void deleteByid(LoanLargeDeliveryManagementSearchResponse response) {
        deleteRedis(response.getType());
        try {
            repository.delete((Long) response.getId());
        } catch (Exception e) {
            LogService.error("删除失败",e);
        }
    }

    @Override
    public Integer updateLoanWholeSaleComment(LoanLargeDeliveryManagementSearchRequest request) {
        deleteRedis(request.getType());
        return repository.updateLoanLargeDeliveryManagement(request);
    }


    @Override
    protected JPAAuditRepository<LoanLargeDeliveryManagement, Long> getRepository() {
        return repository;
    }

    /**
     * 删除投递管理和大额API甲方的所有缓存
     * @param type
     */
    private void deleteRedis(Integer type){
        service.listMapByCode(DictionaryCode.PRODUCT_MONEY).forEach((k, v)->{
            stringRedisTemplate.delete(String.format(LoanRedisKey.LOAN_LARGE_DELIVER_INFO,Integer.parseInt(k)));
        });
        stringRedisTemplate.delete(LoanRedisKey.LOAN_WHOLESALE_COMMENT_ENTITY+type);
        stringRedisTemplate.delete(LoanRedisKey.LOAN_WHOLESALE_COMMENT_ALL);
    }
}
